
/*******************************************************************************
* Copyright (C) JuCheap.Com
* 
* Author: JuCheap
* Create Date: 04/09/2020 14:52:30
* Description: 数据库实例文件(此文件为T4模板生成，请勿改动) 
* 
* Revision History:
* Date         Author               Description
*
*********************************************************************************/

using JuCheap.Core.Data.Entity;
using Microsoft.EntityFrameworkCore;
using JuCheap.Core.Infrastructure.Utilities;

namespace JuCheap.Core.Data
{
    /// <summary>
    /// JuCheapContext
    /// </summary>
    public class JuCheapContext : DbContext
    {
		/// <summary>
		/// 公司Id
		/// </summary>
		private string _companyId;
		/// <summary>
		/// 是否是系统超级管理员
		/// </summary>
		private bool _isSystem;

		/// <summary>
        /// Product
        /// </summary>
		public DbSet<Product> Product { get; set; }
		/// <summary>
        /// ProductCategory
        /// </summary>
		public DbSet<ProductCategory> ProductCategory { get; set; }
		/// <summary>
        /// ProductImage
        /// </summary>
		public DbSet<ProductImage> ProductImage { get; set; }
		/// <summary>
        /// ProductSpec
        /// </summary>
		public DbSet<ProductSpec> ProductSpec { get; set; }
		/// <summary>
        /// SysCompany
        /// </summary>
		public DbSet<SysCompany> SysCompany { get; set; }
		/// <summary>
        /// SysDepartment
        /// </summary>
		public DbSet<SysDepartment> SysDepartment { get; set; }
		/// <summary>
        /// SysLoginLog
        /// </summary>
		public DbSet<SysLoginLog> SysLoginLog { get; set; }
		/// <summary>
        /// SysMenu
        /// </summary>
		public DbSet<SysMenu> SysMenu { get; set; }
		/// <summary>
        /// SysMessage
        /// </summary>
		public DbSet<SysMessage> SysMessage { get; set; }
		/// <summary>
        /// SysMessageUser
        /// </summary>
		public DbSet<SysMessageUser> SysMessageUser { get; set; }
		/// <summary>
        /// SysRole
        /// </summary>
		public DbSet<SysRole> SysRole { get; set; }
		/// <summary>
        /// SysRoleMenu
        /// </summary>
		public DbSet<SysRoleMenu> SysRoleMenu { get; set; }
		/// <summary>
        /// SysUpgradeLog
        /// </summary>
		public DbSet<SysUpgradeLog> SysUpgradeLog { get; set; }
		/// <summary>
        /// SysUpgradeLogDetail
        /// </summary>
		public DbSet<SysUpgradeLogDetail> SysUpgradeLogDetail { get; set; }
		/// <summary>
        /// SysUser
        /// </summary>
		public DbSet<SysUser> SysUser { get; set; }
		/// <summary>
        /// SysUserRole
        /// </summary>
		public DbSet<SysUserRole> SysUserRole { get; set; }
		/// <summary>
        /// SysVisitLog
        /// </summary>
		public DbSet<SysVisitLog> SysVisitLog { get; set; }

        /// <summary>
        /// JuCheapContext
        /// </summary>
        public JuCheapContext(DbContextOptions<JuCheapContext> options, ICurrentUserService currentUserService):
            base(options)
        {
            _companyId = currentUserService.GetCompanyId();
			var user = currentUserService.GetCurrentUser();
			_isSystem = user == null || user?.IsSystemCompany == true;
        }

        /// <summary>
        /// OnModelCreating
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
			//modelBuilder.Entity<Product>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<Product>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<ProductCategory>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<ProductCategory>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<ProductImage>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<ProductImage>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<ProductSpec>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<ProductSpec>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysCompany>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysCompany>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysDepartment>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysDepartment>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysLoginLog>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysLoginLog>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysMenu>().HasQueryFilter(x => !x.IsDeleted);
			modelBuilder.Entity<SysMenu>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysMessage>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysMessage>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysMessageUser>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysMessageUser>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysRole>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysRole>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysRoleMenu>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysRoleMenu>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysUpgradeLog>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysUpgradeLog>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysUpgradeLogDetail>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysUpgradeLogDetail>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysUser>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysUser>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysUserRole>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysUserRole>().HasQueryFilter(x => !x.IsDeleted);
			//modelBuilder.Entity<SysVisitLog>().HasQueryFilter(x => !x.IsDeleted && (_isSystem || x.CompanyId == _companyId));
			modelBuilder.Entity<SysVisitLog>().HasQueryFilter(x => !x.IsDeleted);
            base.OnModelCreating(modelBuilder);
        }

		/// <summary>
        /// OnConfiguring
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
        }
    }
}

